using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using QL_Truong_Hoc.DTO;

namespace QL_Truong_Hoc.DAO
{
	public static class THocSinhMonDAO
	{
		#region Methods

		/// <summary>
		/// Saves a record to the tHocSinhMon table.
		/// </summary>
		public static bool Insert(Database objDb, DbTransaction objTrans, THocSinhMonDTO tHocSinhMon)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("tHocSinhMon", tHocSinhMon);

			object[] parameters = new object[]
			{
				Null.GetNull(tHocSinhMon.HS_ID, DBNull.Value),
				Null.GetNull(tHocSinhMon.MO_ID, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso1, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso2, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso3, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso4, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso5, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso6, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso7, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso8, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso9, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso10, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemketthucmodulelan1, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemketthucmodulelan2, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemtongket, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_ghichu, ""),
				Null.GetNull(tHocSinhMon.HSM_hoc, false)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonInsert", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonInsert", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tHocSinhMonInsert", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Updates a record in the tHocSinhMon table.
		/// </summary>
		public static bool Update(Database objDb, DbTransaction objTrans, THocSinhMonDTO tHocSinhMon)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("tHocSinhMon", tHocSinhMon);

			object[] parameters = new object[]
			{
				Null.GetNull(tHocSinhMon.HS_ID, DBNull.Value),
				Null.GetNull(tHocSinhMon.MO_ID, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso1, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso2, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso3, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso4, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso5, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso6, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso7, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso8, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso9, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemdinhkylanso10, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemketthucmodulelan1, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemketthucmodulelan2, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_diemtongket, DBNull.Value),
				Null.GetNull(tHocSinhMon.HSM_ghichu, ""),
				Null.GetNull(tHocSinhMon.HSM_hoc, false)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonUpdate", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonUpdate", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tHocSinhMonUpdate", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the tHocSinhMon table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, int hS_ID, int mO_ID)
		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{
Null.GetNull(hS_ID, DBNull.Value),
Null.GetNull(mO_ID, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonDelete", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonDelete", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tHocSinhMonDelete", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the tHocSinhMon table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, string strWhere)		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{strWhere
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonDeleteWhere", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("tHocSinhMonDeleteWhere", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "tHocSinhMonDeleteWhere", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Selects a single record from the tHocSinhMon table.
		/// </summary>
		public static THocSinhMonDTO Select(Database objDb, int hS_ID, int mO_ID)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(hS_ID, DBNull.Value),
Null.GetNull(mO_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhMonSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects a single record from the tHocSinhMon table.
		/// </summary>
		public static DataTable Select(Database objDb, int hS_ID, int mO_ID, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(hS_ID, DBNull.Value),
Null.GetNull(mO_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhMonSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Get next index from the tHocSinhMon table.
		/// </summary>
		public static int SelectNextIndex(Database objDb)		{
int intResult = -1;
try{
IDataReader dr = objDb.ExecuteReader("tHocSinhMonSelectNextIndex");
if (dr != null)
{
DataTable dt = Untils.ConvertDataReaderToDataTable(dr, true);
intResult = clsCommon.fncCnvNullToInt(dt.Rows[0][0]);
}
}catch { }
return intResult;
		}

		/// <summary>
		/// Selects all records from the tHocSinhMon table.
		/// </summary>
		public static List<THocSinhMonDTO> SelectAll(Database objDb)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhMonSelectAll"))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tHocSinhMon table.
		/// </summary>
		public static List<THocSinhMonDTO> SelectWhere(Database objDb, string strWhere)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhMonSelectWhere", parameters))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tHocSinhMon table.
		/// </summary>
		public static DataTable SelectAll(Database objDb, bool blnReturnDatatable = true)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhMonSelectAll"))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the tHocSinhMon table.
		/// </summary>
		public static DataTable SelectWhere(Database objDb, string strWhere, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("tHocSinhMonSelectWhere", parameters))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Creates a new instance of the THocSinhMonDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static THocSinhMonDTO MapDataReader(IDataReader dataReader)
		{
			THocSinhMonDTO tHocSinhMonDTO = new THocSinhMonDTO();
DataTable dt = new DataTable();
dt.Load(dataReader);
if (dt.Rows.Count <= 0) return tHocSinhMonDTO;
			tHocSinhMonDTO.HS_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["HS_ID"]);
			tHocSinhMonDTO.MO_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["MO_ID"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso1 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["HSM_diemdinhkylanso1"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso2 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["HSM_diemdinhkylanso2"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso3 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["HSM_diemdinhkylanso3"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso4 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["HSM_diemdinhkylanso4"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso5 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["HSM_diemdinhkylanso5"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso6 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["HSM_diemdinhkylanso6"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso7 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["HSM_diemdinhkylanso7"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso8 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["HSM_diemdinhkylanso8"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso9 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["HSM_diemdinhkylanso9"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso10 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["HSM_diemdinhkylanso10"]);
			tHocSinhMonDTO.HSM_diemketthucmodulelan1 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["HSM_diemketthucmodulelan1"]);
			tHocSinhMonDTO.HSM_diemketthucmodulelan2 = clsCommon.fncCnvNullToFloat(dt.Rows[0]["HSM_diemketthucmodulelan2"]);
			tHocSinhMonDTO.HSM_diemtongket = clsCommon.fncCnvNullToFloat(dt.Rows[0]["HSM_diemtongket"]);
			tHocSinhMonDTO.HSM_ghichu = clsCommon.fncCnvNullToString(dt.Rows[0]["HSM_ghichu"]);
			tHocSinhMonDTO.HSM_hoc = clsCommon.fncCnvNullToBool(dt.Rows[0]["HSM_hoc"]);

			return tHocSinhMonDTO;
		}
		/// <summary>
		/// Creates a new instance of the THocSinhMonDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static List<THocSinhMonDTO> MapDataReaderList(IDataReader dataReader)
		{
				List<THocSinhMonDTO> lstResult = new List<THocSinhMonDTO>();
			THocSinhMonDTO tHocSinhMonDTO;
DataTable dt = new DataTable();
dt.Load(dataReader);
for (int i = 0; i <= dt.Rows.Count-1; i++)
{
			tHocSinhMonDTO = new THocSinhMonDTO();
			tHocSinhMonDTO.HS_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["HS_ID"]);
			tHocSinhMonDTO.MO_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["MO_ID"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso1 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["HSM_diemdinhkylanso1"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso2 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["HSM_diemdinhkylanso2"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso3 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["HSM_diemdinhkylanso3"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso4 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["HSM_diemdinhkylanso4"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso5 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["HSM_diemdinhkylanso5"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso6 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["HSM_diemdinhkylanso6"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso7 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["HSM_diemdinhkylanso7"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso8 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["HSM_diemdinhkylanso8"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso9 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["HSM_diemdinhkylanso9"]);
			tHocSinhMonDTO.HSM_diemdinhkylanso10 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["HSM_diemdinhkylanso10"]);
			tHocSinhMonDTO.HSM_diemketthucmodulelan1 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["HSM_diemketthucmodulelan1"]);
			tHocSinhMonDTO.HSM_diemketthucmodulelan2 = clsCommon.fncCnvNullToFloat(dt.Rows[i]["HSM_diemketthucmodulelan2"]);
			tHocSinhMonDTO.HSM_diemtongket = clsCommon.fncCnvNullToFloat(dt.Rows[i]["HSM_diemtongket"]);
			tHocSinhMonDTO.HSM_ghichu = clsCommon.fncCnvNullToString(dt.Rows[i]["HSM_ghichu"]);
			tHocSinhMonDTO.HSM_hoc = clsCommon.fncCnvNullToBool(dt.Rows[i]["HSM_hoc"]);
lstResult.Add(tHocSinhMonDTO);
}

			return lstResult;
		}

		#endregion
	}
}
